﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Orchard.Themes;
using Hub.Models;
using Orchard.UI.Admin;
using Hub.Services;

namespace Hub.Controllers {

    [Themed]
    public class PublishController : Controller {

        private readonly IHubService _hubService;

        public PublishController(IHubService hubService) {
            _hubService = hubService;
        }

        [HttpGet]
        public ActionResult Index() {
            return View();
        }

        [HttpGet]
        public ActionResult EditMapping(int? id) {
            if (id.HasValue) {
                var content = _hubService.RetriveMapping(id.Value);
                return View(content);
            }
            return View();
        }

        [HttpPost]
        public ActionResult EditMapping(ShareContentPartRecord mapping) {
            if (ModelState.IsValid) {
                _hubService.UpdateContent(mapping);
                return RedirectToAction("ViewAll");
            }
            return View();
        }

        [HttpPost]
        public ActionResult SaveMapping(string pictureUrl, string t, string d) {
            _hubService.PublishContent(new ShareContentPartRecord
            {
                Title = t,
                Description = d,
                PictureUrl = pictureUrl
            });

            return Content("true");
        }

        [HttpGet]
        public ActionResult DeleteMapping(int? id) {
            if (id.HasValue) {
                var content = _hubService.RetriveMapping(id.Value);
                _hubService.DeleteContent(content);
                return RedirectToAction("ViewAll");
            }
            return RedirectToAction("ViewAll");
        }

        [HttpGet]
        public ActionResult ViewAll() {
            var result = _hubService.RetriveAll();
            return View(result);
        }

    }
}
